package com.matrix.java.chap_06.example;

import java.util.Scanner;

public class Case6_08 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("十六进制数: ");
        String Hex = input.next();
        System.out.println("十进制数: " + Hex2Dec(Hex.toUpperCase()));
        input.close();
    }

    private static int Hex2Dec(String hex) {
        int dec = 0;
        for (int i = 0; i < hex.length(); i++) {
            dec = dec * 16 + char2num(hex.charAt(i));
        }
        return dec;
    }

    private static int char2num(char ch) {
        if (ch >= 'A' && ch <= 'F') {
            return 10 + ch - 'A';
        } else {
            return ch - '0';
        }
    }
}
